package com.tbit.uqbike.service.event.handler;

import com.tbit.uqbike.constant.BrandRemoteConfigConstant;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.service.business.BrandRemoteConfigService;
import com.tbit.uqbike.service.business.UserService;
import com.tbit.uqbike.service.event.EventType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 免登录配置变更处理
 *
 *
 * @author <a href="mailto:chen.chaoxiong@tbit.com.cn">ChenChaoXiong</a>
 * @since 2024/5/16
 */
@Component
@Slf4j
public class NoLoginEventHandler extends EventHandler {

    @Autowired
    private UserService userService;

    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;

    @Override
    protected boolean match(Object config, EventType eventType) {
        if (Objects.isNull(config) || !(config instanceof BrandRemoteConfig)){
            return false;
        }
        BrandRemoteConfig brandRemoteConfig = (BrandRemoteConfig) config;
        return BrandRemoteConfigConstant.NO_LOGIN_EXPIRY_DAYS.equals(brandRemoteConfig.getConfigType());
    }

    /**
     * 处理免登录配置变更事件
     * 新增 修改 删除 时让用户重新登录获取最新配置
     * @param config    配置
     * @param eventType 事件类型
     */
    @Override
    public void doHandle(Object config, EventType eventType) {
        if (!(config instanceof  BrandRemoteConfig)){
            return;
        }
        BrandRemoteConfig brandRemoteConfig = (BrandRemoteConfig) config;
        log.info("处理免登录配置变更事件：{},事件类型:{}", config, eventType);
        Integer accountId = brandRemoteConfig.getAccountId();
        if (Objects.isNull(accountId)) {
            return;
        }
        brandRemoteConfigService.removeCache(accountId, BrandRemoteConfigConstant.NO_LOGIN_EXPIRY_DAYS);
    }
}
